﻿<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Update Request</Title>
            <Author>FSH</Author>
            <Description>Adds boilerplate code for update request</Description>
            <Shortcut>updaterequest</Shortcut>
        </Header>
        <Snippet>
            <References>
                <Reference>
                </Reference>
            </References>
            <Imports>
                <Import>
                    <Namespace>FSH.WebApi.Domain.Common.Events</Namespace>
                </Import>
            </Imports>
            <Declarations>
                <Literal>
                    <ID>classname</ID>
                    <ToolTip>Replace with specified name</ToolTip>
                    <Default>Entity</Default>
                </Literal>
                <Literal>
                    <ID>entityclassname</ID>
                    <ToolTip>Replace with specified name</ToolTip>
                    <Default>Entity</Default>
                </Literal>
                <Object>
                    <ID>Object</ID>
                    <Type>System.Object</Type>
                    <ToolTip>Replace with a object in your application.</ToolTip>
                    <Default>myObject</Default>
                </Object>
            </Declarations>
            <Code Language="CSharp">
                <![CDATA[ 
public class Update$classname$Request : IRequest<Guid>
{
    public Guid Id { get; set; }
}

public class Update$classname$RequestHandler : IRequestHandler<Update$classname$Request,Guid>
{
    // Add Domain Events automatically by using IRepositoryWithEvents
    private readonly IRepositoryWithEvents<$entityclassname$> _repository;
    private readonly IStringLocalizer _t;

    public Update$classname$RequestHandler(IRepositoryWithEvents<$entityclassname$> repository, IStringLocalizer<Update$classname$RequestHandler> localizer) =>
        (_repository, _t) = (repository, localizer);

    public async Task<Guid> Handle(Update$classname$Request request, CancellationToken cancellationToken)
    {
        var entity = await _repository.GetByIdAsync(request.Id, cancellationToken);

        _ = entity
        ?? throw new NotFoundException(_t["Entity {0} Not Found.", request.Id]);

        entity.Update(/*Add properties*/);

        await _repository.UpdateAsync(entity, cancellationToken);

        return request.Id;
    }
} 
]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>